package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.WheelViewAdapter;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.BeiJianDanWei;
import com.cz.nongyetongv5.bean.ChanPinJianCeRecord;
import com.cz.nongyetongv5.bean.ChanPinLeiBie;
import com.cz.nongyetongv5.bean.ChouYanJiGou;
import com.cz.nongyetongv5.bean.Dict;
import com.cz.nongyetongv5.bean.Duixiangleixing;
import com.cz.nongyetongv5.bean.JianCeFangFa;
import com.cz.nongyetongv5.bean.JianCeLeiBie;
import com.cz.nongyetongv5.bean.JianCeYiJu;
import com.cz.nongyetongv5.bean.JianChaHangYe;
import com.cz.nongyetongv5.bean.JianChaRenYuan;
import com.cz.nongyetongv5.bean.Kv;
import com.cz.nongyetongv5.bean.Plan;
import com.cz.nongyetongv5.bean.TextInfo;
import com.cz.nongyetongv5.bean.Town;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.bean.YangPin;
import com.cz.nongyetongv5.custom.wheelview.TosAdapterView;
import com.cz.nongyetongv5.custom.wheelview.TosAdapterView.OnItemSelectedListener;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.custom.wheelview.WheelView;
import com.cz.nongyetongv5.utils.AsyncClienter;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.Conf;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.DialogUtil;
import com.cz.nongyetongv5_1.adapter.ListAdapter;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ChanPinJianCeSelEntActivity extends Activity implements
		OnClickListener {
	public static String pid=null;
	private ViewGroup rl_chanpinjiance_item_suoshuquyu = null;

	/**
	 * 受检单位
	 */
	private ViewGroup rl_chanpinjiance_item_shoujiandanwei = null;

	/**
	 * 对象类型
	 */
	protected ViewGroup rl_jiandujiancha_item_duixiangleixing = null;
	/**
	 * 检查行业
	 */
	private ViewGroup rl_jiandujiancha_item_jianchahangye = null;








	/**
	 * 受检单位的布局
	 */
	private ViewGroup llCreateShouJianDanWeiLayout = null;


	/**
	 * 数据库帮助对象
	 */
	private DatabaseHelper db = null;

	/**
	 * 数据对象
	 */
	private DataBean dataBean = new DataBean();

	/**
	 * 用户对象
	 */
	private User user = null;

	/**
	 * 确认对象
	 */
	private SureExecuter sureExecuter = new SureExecuter();

	private boolean isEditMode = false;




	JSONObject plan=null;

	private ConfigUtil configUtil = null;


	/**
	 * 初始化视图
	 */
	private void initView() {

		plan=ChouyangrenwuActivity.getCheckPlan();
		rl_jiandujiancha_item_duixiangleixing= (ViewGroup) this
				.findViewById(R.id.rl_jiandujiancha_item_duixiangleixing);

		// 查询出检查行业
		rl_jiandujiancha_item_jianchahangye = (ViewGroup) this
				.findViewById(R.id.rl_jiandujiancha_item_jianhangye);
		rl_jiandujiancha_item_jianchahangye.setOnClickListener(this);
		rl_jiandujiancha_item_duixiangleixing.setOnClickListener(this);


		rl_chanpinjiance_item_suoshuquyu = (ViewGroup) this
				.findViewById(R.id.rl_chanpinjiance_item_suoshuquyu);
		rl_chanpinjiance_item_suoshuquyu.setOnClickListener(this);





		rl_chanpinjiance_item_shoujiandanwei = (ViewGroup) this
				.findViewById(R.id.rl_chanpinjiance_item_shoujiandanwei);
		rl_chanpinjiance_item_shoujiandanwei.setOnClickListener(this);




		llCreateShouJianDanWeiLayout = (ViewGroup) this.findViewById(
				R.id.llCreateShouJianDanWeiLayout);
		llCreateShouJianDanWeiLayout.setOnClickListener(this);

		this.findViewById(R.id.btnBack).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.putExtra("type", "0");
				setResult(
						ChanPinJianCePlanRecordActivity.TO_SELENTS,
						intent);
				finish();
			}



		});

		this.findViewById(R.id.btnSure).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {

				if(dataBean.data.getShouJianDanWei()==null){
					DialogUtil.showMessageDialog("请填写受检单位..", null, ChanPinJianCeSelEntActivity.this);
					return;
				}

				Intent intent = new Intent();



				Bundle bundle = new Bundle();
				bundle.putSerializable("ent", dataBean.data.getShouJianDanWei());
				bundle.putSerializable("towns", dataBean.data.getTowns());

				intent.putExtra("data",bundle);
				intent.putExtra("type", "1");
				setResult(
						ChanPinJianCePlanRecordActivity.TO_SELENTS,
						intent);
				finish();
			}



		});

	}
	private String planId="0";
	/**
	 * 初始化配置
	 */
	private void initConfig() {

		db = new DatabaseHelper(this, CommonConstants.DBNAME, null,
				CommonConstants.DATABASE_VERSION);

		user = (User) this.getIntent().getSerializableExtra("user");

		planId= String.valueOf(this.getIntent().getSerializableExtra("planId"));
		dataBean.data = new ChanPinJianCeRecord();
		dataBean.data.setUsername(this.user.getUsername());




		/*
		 * 根据用户信息进行区域的初始化
		 */

		if (user.getCityId() != null) {
			dataBean.getData().getTowns()[0] = db.queryTownById(user
					.getCityId());
		}

		if (user.getArea() != null) {
			dataBean.getData().getTowns()[1] = db.queryTownById(user.getArea());

		}

		if (user.getTownId() != null) {
			dataBean.getData().getTowns()[2] = db.queryTownById(user
					.getTownId());

		}
		if (user.getStreetId() != null) {
			dataBean.getData().getTowns()[3] = db.queryTownById(user
					.getStreetId());

		}

		/**
		 * 自动填写区域
		 */
		sureExecuter.sureQuYu();
		sureExecuter.sureJianChaHangYe();
		sureExecuter.sureDuxiangleixing();


		checkPlan(planId);
		BeiJianDanWei bj= (BeiJianDanWei)getIntent().getSerializableExtra("ent");
		if(bj!=null)
			dataBean.data.setShouJianDanWei(bj);
		sureExecuter.sureShouJianDanWei();



	}
	private void checkPlan(String id){
		Plan plan = db
				.queryPlanByID(id);
		if (plan!=null){
			dataBean.data.setPlan(plan);
			dataBean.data.setChanPinLeiBie(null);
			dataBean.getData().setDuixiangleixing(new ArrayList<Duixiangleixing>());
			sureExecuter.sureDuxiangleixing();

			clearBeijingdanwei();



			String plancode=plan.getEntArea();

			Log.d("rafe_dr",plancode);

			dataBean.getData().setTowns(new Town[4]);
			if(plancode.length()>3){
				Town town1=db.queryTownById(plancode.substring(0, 4));
				if(town1!=null)dataBean.getData().getTowns()[0]=town1;
			}
			if(plancode.length()>5){
				Town town1=db.queryTownById(plancode.substring(0, 6));
				if(town1!=null)dataBean.getData().getTowns()[1]=town1;
			}
			if(plancode.length()>7){
				Town town1=db.queryTownById(plancode.substring(0,8));
				if(town1!=null)dataBean.getData().getTowns()[2]=town1;
			}
			if(plancode.length()>9){
				Town town1=db.queryTownById(plancode.substring(0,10));
				if(town1!=null)dataBean.getData().getTowns()[3]=town1;
			}

			String[] k11=dataBean.data.getPlan().arrayEntKinds();
			if(k11!=null&&k11.length==1){
				if(!k11[0].equals("[]")){
					List<Kv> k12 =Dict.getKinds(k11);
					if(k12!=null&&k12.size()>0){
						Duixiangleixing temp=new Duixiangleixing(k12.get(0).getId(),k12.get(0).getName());
						dataBean.getData().setDuixiangleixing(new ArrayList<Duixiangleixing>());
						dataBean.getData().getDuixiangleixing().add(temp);
						sureExecuter.sureDuxiangleixing();
					}
				}
			}




			String[] ps1=dataBean.getPlan().arrayEntTrade();
			if(ps1.length==1){

				if(!ps1[0].equals("[]")){
					dataBean.data.getJianChaHangYes().clear();
					List<JianChaHangYe> hys = db.queryAllJianChaHangYe();
					for(int i=0;i<hys.size();i++){
						if(ps1[0].equals(hys.get(i).getId().toString())){
							dataBean.data.getJianChaHangYes().add(hys.get(i));
						}
					}
					sureExecuter.sureJianChaHangYe();
				}
			}

			sureExecuter.sureQuYu();
			//SerliazableUtil.saveTemp(dataBean.data, user.getUsername());
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.v1_4_chanpinchouyang_ent);
		configUtil = new ConfigUtil(getResources());

		initView();

		initConfig();

	}

	@Override
	public void onClick(View v) {

		final AlertDialog bottomWindow = new AlertDialog.Builder(
				new ContextThemeWrapper(this,
						R.style.NongYeTongFullscreenTheme)).create();

		// 输入法的设置
		bottomWindow.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

		// 设置窗口的动画
		bottomWindow.getWindow().setWindowAnimations(R.style.bottomAnimStyle);
		//对象类型
		if(v==rl_jiandujiancha_item_duixiangleixing){
			onClickDuixiangleixing(bottomWindow );
		}

		// 当点击检查行业的时候
		if (v == rl_jiandujiancha_item_jianchahangye) {



			bottomWindow.show();

			/**
			 * 检查行业底部的布局
			 */
			ViewGroup bottomJianChaHangYeWindowLayout = (ViewGroup) this
					.getLayoutInflater().inflate(
							R.layout.bottom_jianchahangye, null);

			// 让底部的宽度和屏幕的宽度一致
			bottomJianChaHangYeWindowLayout.setMinimumWidth(this
					.getWindowManager().getDefaultDisplay().getWidth());

			// 设置底部布局
			bottomWindow.setContentView(bottomJianChaHangYeWindowLayout);

			// 弹出输入法的操作
			bottomWindow.getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

			bottomWindow.getWindow().setGravity(Gravity.BOTTOM);

			/**
			 * 进行视图的动态生成
			 */
			ViewGroup jianChaHangYeLayout = (ViewGroup) bottomJianChaHangYeWindowLayout
					.findViewById(R.id.jianchayangyelayout);
			// 清理视图
			jianChaHangYeLayout.removeAllViews();

			// 查询出所有检查行业
			List<JianChaHangYe> jianChaHangYes = db.queryAllJianChaHangYe();

			List<JianChaHangYe> jianChaHangYes2 = new ArrayList<JianChaHangYe>();
			if(plan!=null){
				for(int i=0;i<jianChaHangYes.size();i++){
					String tr=plan.optString("trades")+",";
					if(tr.indexOf(jianChaHangYes.get(i).getId()+",")>=0){
						jianChaHangYes2.add(jianChaHangYes.get(i));
					}
				}
				jianChaHangYes=jianChaHangYes2;

			}



			final List<CheckBox> cbsJianChaHangYe = new ArrayList<CheckBox>();

			// 遍历检查行业进行视图的生成
			for (final JianChaHangYe temp : jianChaHangYes) {

				final RelativeLayout rootLayout = new RelativeLayout(
						this);

				MarginLayoutParams rootLayoutPrams = new MarginLayoutParams(
						MarginLayoutParams.MATCH_PARENT,
						MarginLayoutParams.WRAP_CONTENT);

				rootLayoutPrams.setMargins(0,
						Utils.dip2px(this, 3), 0,
						Utils.dip2px(this, 3));

				// 兼容低版本设置margin方法
				LayoutParams layoutPrarams = new LayoutParams(
						rootLayoutPrams);
				layoutPrarams.gravity = Gravity.TOP | Gravity.LEFT;

				rootLayout.setLayoutParams(layoutPrarams);
				rootLayout.setClickable(true);
				rootLayout.setGravity(Gravity.CENTER_VERTICAL);
				rootLayout.setPadding(Utils.dip2px(this, 8),
						Utils.dip2px(this, 5),
						Utils.dip2px(this, 8),
						Utils.dip2px(this, 5));
				rootLayout
						.setBackgroundResource(R.drawable.jiandujiancha_item_style);

				/*********************************************/
				TextView tvBeiJianChaHangYe = new TextView(this);

				RelativeLayout.LayoutParams tvBeiJianChaHangYeLayout = new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.WRAP_CONTENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT);
				tvBeiJianChaHangYeLayout
						.addRule(RelativeLayout.CENTER_VERTICAL);
				tvBeiJianChaHangYe
						.setLayoutParams(tvBeiJianChaHangYeLayout);

				tvBeiJianChaHangYe.setPadding(
						Utils.dip2px(this, 8),
						Utils.dip2px(this, 8),
						Utils.dip2px(this, 8),
						Utils.dip2px(this, 8));
				tvBeiJianChaHangYe
						.setTextColor(Color.parseColor("#656769"));
				tvBeiJianChaHangYe.setTextSize(20);
				tvBeiJianChaHangYe.setText(temp.getName());

				rootLayout.addView(tvBeiJianChaHangYe);

				/*********************************************/

				CheckBox cbBeiJianChaHangYe = new CheckBox(this);

				cbBeiJianChaHangYe.setTag(temp);

				RelativeLayout.LayoutParams cbBeiJianChaHangYeLayout = new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.WRAP_CONTENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT);
				cbBeiJianChaHangYeLayout
						.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				cbBeiJianChaHangYeLayout
						.addRule(RelativeLayout.CENTER_VERTICAL);
				cbBeiJianChaHangYe
						.setLayoutParams(cbBeiJianChaHangYeLayout);

				if (dataBean.data.getJianChaHangYes().contains(temp)) {
					cbBeiJianChaHangYe.setChecked(true);

				} else {

					cbBeiJianChaHangYe.setChecked(false);
				}

				cbBeiJianChaHangYe
						.setButtonDrawable(R.drawable.checkbox_select_style);

				rootLayout.addView(cbBeiJianChaHangYe);

				rootLayout.setTag(cbBeiJianChaHangYe);

				// 增加到缓存集合中
				cbsJianChaHangYe.add(cbBeiJianChaHangYe);

				jianChaHangYeLayout.addView(rootLayout);

				// 绑定事件,当布局被点击的时候选中checkbox
				rootLayout.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						if (rootLayout.getTag() instanceof CheckBox) {

							CheckBox cbsJianChaHangYe = (CheckBox) rootLayout
									.getTag();

							// 将指定项选中
							if (cbsJianChaHangYe.isChecked()) {
								cbsJianChaHangYe.setChecked(false);
							} else {
								cbsJianChaHangYe.setChecked(true);
							}

						}

					}
				});

			}

			// 底部Window 的返回按钮
			ViewGroup btnBottomBack = (ViewGroup) bottomJianChaHangYeWindowLayout
					.findViewById(R.id.btnBack);

			btnBottomBack.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					bottomWindow.dismiss();
				}
			});

			// 选择区域底部的确定按钮
			ViewGroup btnSureJianChaHangYe = (ViewGroup) bottomJianChaHangYeWindowLayout
					.findViewById(R.id.btnSureJianChaHangYe);

			btnSureJianChaHangYe.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dataBean.data.getJianChaHangYes().clear();

					for (CheckBox checkBox : cbsJianChaHangYe) {

						if (checkBox.getTag() instanceof JianChaHangYe
								&& checkBox.isChecked()) {

							JianChaHangYe hangYe = (JianChaHangYe) checkBox
									.getTag();

							dataBean.data.getJianChaHangYes().add(hangYe);
						}

					}


					sureExecuter.sureJianChaHangYe();
					clearBeijingdanwei();
					//SerliazableUtil.saveTemp(dataBean.data, user.getUsername());
					bottomWindow.dismiss();
				}
			});

		}
		/**
		 * 产品检测所属区域
		 */
		Log.d("debug_data",String.valueOf(plan));
		boolean show=plan==null;

		if (v == rl_chanpinjiance_item_suoshuquyu&&show) {
			ViewGroup bottomXuanZeQuYuWindowLayout = (ViewGroup) LayoutInflater
					.from(this).inflate(R.layout.bottom_suanshuquyu2,
							null);

			bottomWindow.show();

			// 让底部的宽度和屏幕的宽度一致
			bottomXuanZeQuYuWindowLayout.setMinimumWidth(this
					.getWindowManager().getDefaultDisplay().getWidth());

			// 设置底部布局
			bottomWindow.setContentView(bottomXuanZeQuYuWindowLayout);

			// 弹出输入法的操作
			bottomWindow.getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

			// 设置到底部
			bottomWindow.getWindow().setGravity(Gravity.BOTTOM);

			/**
			 * 加载所选区域的三个WheelView
			 */
			final WheelView wvCity = (WheelView) bottomXuanZeQuYuWindowLayout
					.findViewById(R.id.wdCity);

			WheelViewAdapter adpCity = new WheelViewAdapter(this);

			/**
			 * 增加市的数据,根据用户信息查询单个
			 */
			List<Town> citys = new ArrayList<Town>();

			ArrayList<TextInfo> wvCityDatas = new ArrayList<TextInfo>();

			citys.add(dataBean.getData().getTowns()[0]);

			wvCity.setEnabled(false);

			for (Town town : citys) {

				if (town != null) {
					// 添加市的数据
					wvCityDatas.add(new TextInfo(town.getCode(), town
							.getName(), town));
				}

			}

			adpCity.setData(wvCityDatas);

			wvCity.setAdapter(adpCity);

			/**
			 * 加载所选区域的三个WheelView
			 */
			final WheelView wvArea = (WheelView) bottomXuanZeQuYuWindowLayout
					.findViewById(R.id.wdArea);

			WheelViewAdapter adpArea = new WheelViewAdapter(this);

			/**
			 * 增加区域的数据,根据用户信息查询单个
			 */
			List<Town> areas = db.queryTownByParent(dataBean.getData()
					.getTowns()[0].getCode());

			final ArrayList<TextInfo> wvAreaDatas = new ArrayList<TextInfo>();

			if (dataBean.getData().getTowns()[1] != null
					&& user.getArea() != null) {
				wvArea.setEnabled(false);
			} else {
				wvAreaDatas.add(new TextInfo(-1, "[请选择]", null));

			}

			for (Town town : areas) {
				// 添加市的数据
				wvAreaDatas.add(new TextInfo(town.getCode(),
						town.getName(), town));
			}

			adpArea.setData(wvAreaDatas);

			wvArea.setAdapter(adpArea);

			TextInfo areaFilter = null;

			if (dataBean.getData().getTowns()[1] != null) {

				areaFilter = new TextInfo(
						dataBean.getData().getTowns()[1].getCode(), null);
			} else {
				areaFilter = new TextInfo(null, null);
			}

			int position = wvAreaDatas.indexOf(areaFilter);

			position = position < 0 ? 0 : position;

			wvArea.setSelection(position);

			/**
			 * 加载所选区域的三个WheelView
			 */
			final WheelView wvTown = (WheelView) bottomXuanZeQuYuWindowLayout
					.findViewById(R.id.wdTown);

			final WheelViewAdapter adpTown = new WheelViewAdapter(
					this);

			TextInfo areaInfo = (TextInfo) wvArea.getSelectedItem();

			/**
			 * 增加乡镇的数据
			 */
			List<Town> towns = db.queryTownByParent("" + areaInfo.id);

			final ArrayList<TextInfo> wvTownDatas = new ArrayList<TextInfo>();

			if (dataBean.getData().getTowns()[2] != null
					&& user.getTownId() != null) {
				wvTown.setEnabled(false);
			} else {
				wvTownDatas.add(new TextInfo(-1, "[请选择]", null));
			}

			for (Town town : towns) {

				// 添加市的数据
				wvTownDatas.add(new TextInfo(town.getCode(),
						town.getName(), town));

			}

			adpTown.setData(wvTownDatas);

			wvTown.setAdapter(adpTown);

			if (dataBean.getData().getTowns()[2] == null) {
				wvTown.setSelection(0);
			} else {

				TextInfo townFilter = new TextInfo(dataBean.getData()
						.getTowns()[2].getCode(), null);

				position = wvTownDatas.indexOf(townFilter);

				wvTown.setSelection(position);
			}

			wvArea.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(TosAdapterView<?> parent,
										   View view, int position, long id) {

					TextInfo textInfo = wvAreaDatas.get(position);

					wvTownDatas.clear();

					/**
					 * 增加乡镇的数据
					 */
					List<Town> towns = db.queryTownByParent(""
							+ textInfo.id);

					wvTownDatas.add(0, new TextInfo(-1, "[请选择]", null));

					for (Town town : towns) {

						// 添加市的数据
						wvTownDatas.add(new TextInfo(town.getCode(), town
								.getName(), town));

					}

					adpTown.notifyDataSetChanged();

					if (wvTownDatas.size() > 1) {

						wvTown.setSelection(1);
					}

				}

				@Override
				public void onNothingSelected(TosAdapterView<?> parent) {
				}
			});

			//aaa
			/**
			 * 加载所选区域的三个WheelView
			 */
			bottomXuanZeQuYuWindowLayout.findViewById(R.id.wdStreetline).setVisibility(View.VISIBLE);
			final WheelView wvStreet = (WheelView) bottomXuanZeQuYuWindowLayout
					.findViewById(R.id.wdStreet);
			wvStreet.setVisibility(View.VISIBLE);
			final WheelViewAdapter adpStreet = new WheelViewAdapter(
					this);

			TextInfo townInfo = (TextInfo) wvTown.getSelectedItem();

			/**
			 * 增加街道的数据
			 */
			List<Town> streets = db.queryTownByParent("" + townInfo.id);

			final ArrayList<TextInfo> wvStreetDatas = new ArrayList<TextInfo>();

			if (dataBean.getData().getTowns()[3] != null
					&& user.getStreetId() != null) {
				wvStreet.setEnabled(false);
			} else {
				wvStreetDatas.add(new TextInfo(-1, "[请选择]", null));
			}

			for (Town town : streets) {

				// 添加市的数据
				wvStreetDatas.add(new TextInfo(town.getCode(), town
						.getName(), town));

			}

			adpStreet.setData(wvStreetDatas);

			wvStreet.setAdapter(adpStreet);

			if (dataBean.getData().getTowns()[3] == null) {
				wvStreet.setSelection(0);
			} else {

				TextInfo townFilter = new TextInfo(dataBean.getData()
						.getTowns()[3].getCode(), null);

				position = wvStreetDatas.indexOf(townFilter);

				wvStreet.setSelection(position);
			}

			wvTown.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(TosAdapterView<?> parent,
										   View view, int position, long id) {

					TextInfo textInfo = wvTownDatas.get(position);
					wvStreetDatas.clear();
					/**
					 * 增加乡镇的数据
					 */
					List<Town> towns = db.queryTownByParent(""
							+ textInfo.id);

					wvStreetDatas.add(0, new TextInfo(-1, "[请选择]", null));

					for (Town town : towns) {
						// 添加市的数据
						wvStreetDatas.add(new TextInfo(town.getCode(), town
								.getName(), town));

					}
					adpStreet.notifyDataSetChanged();


					if (wvStreetDatas.size() > 1) {
						wvStreet.setSelection(1);
					}

				}

				@Override
				public void onNothingSelected(TosAdapterView<?> parent) {
				}
			});


			// 底部Window 的返回按钮
			ViewGroup btnBottomBack = (ViewGroup) bottomXuanZeQuYuWindowLayout
					.findViewById(R.id.btnBack);

			ViewGroup btnSureXuanZeQuYu = (ViewGroup) bottomXuanZeQuYuWindowLayout
					.findViewById(R.id.btnSureXuanZeQuYu);

			btnBottomBack.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					bottomWindow.dismiss();
				}
			});

			btnSureXuanZeQuYu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					TextInfo cityTextInfo = (TextInfo) wvCity.getSelectedItem();

					if (cityTextInfo.data instanceof Town) {
						dataBean.getData().getTowns()[0] = (Town) cityTextInfo.data;

					} else {
						dataBean.getData().getTowns()[0] = null;

					}

					TextInfo areaTextInfo = (TextInfo) wvArea.getSelectedItem();

					if (areaTextInfo.data instanceof Town) {

						dataBean.getData().getTowns()[1] = (Town) areaTextInfo.data;

					} else {
						dataBean.getData().getTowns()[1] = null;
					}

					TextInfo townTextInfo = (TextInfo) wvTown.getSelectedItem();

					if (townTextInfo.data instanceof Town) {
						dataBean.getData().getTowns()[2] = (Town) townTextInfo.data;
					} else {
						dataBean.getData().getTowns()[2] = null;
					}


					TextInfo wvStreetd = (TextInfo) wvStreet.getSelectedItem();

					if (wvStreetd.data instanceof Town) {
						dataBean.getData().getTowns()[3] = (Town) wvStreetd.data;
					} else {
						dataBean.getData().getTowns()[3] = null;
					}

					sureExecuter.sureQuYu();

					dataBean.data.setShouJianDanWei(null);

					sureExecuter.sureShouJianDanWei();

					//SerliazableUtil.saveTemp(dataBean.data, user.getUsername());

					bottomWindow.dismiss();
				}
			});

		}





		/**
		 * 点击抽样单位的时候rafe ent
		 */
		if (rl_chanpinjiance_item_shoujiandanwei == v) {

			if(!dataBean.checkQuYu()){
				DialogUtil.showMessageDialog("请选择所属区域到区镇级..", null, this);
				return;
			}

			bottomWindow.show();
			page=0;
			// 让底部的宽度和屏幕的宽度一致
			ViewGroup bottomBeiJianDanWeiWindowLayout = (ViewGroup) this
					.getLayoutInflater().inflate(R.layout.bottom_beijiandanwei2,
							null);

			bottomBeiJianDanWeiWindowLayout.setMinimumWidth(this
					.getWindowManager().getDefaultDisplay().getWidth());

			// 设置底部布局
			bottomWindow.setContentView(bottomBeiJianDanWeiWindowLayout);

			// 弹出输入法的操作
			bottomWindow.getWindow().clearFlags(
					WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

			bottomWindow.getWindow().setGravity(Gravity.BOTTOM);

			// 进行标题的绑定
			TextView tvTitle = (TextView) bottomBeiJianDanWeiWindowLayout
					.findViewById(R.id.tvTitle);
			tvTitle.setText(getString(R.string.chanpinjiance_shoujiandanwei_title));


			/**
			 * 加载被检单位
			 */
			final ListView wvBeiJianDanWei=(ListView)bottomBeiJianDanWeiWindowLayout.findViewById(R.id.dataview);



			adpBeiJianDanWeiAdapter = new ListAdapter( this);
			wvBeiJianDanWei.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
										int arg2, long arg3) {
					wvBeiJianDanWei.setTag(arg2);
					adpBeiJianDanWeiAdapter.setSelectItem(arg2);
					adpBeiJianDanWeiAdapter.notifyDataSetInvalidated();

				}});
			wvBeiJianDanWei.setAdapter(adpBeiJianDanWeiAdapter);


			etBeiJianDanWeiFilter = (EditText) bottomBeiJianDanWeiWindowLayout
					.findViewById(R.id.etBeiJianDanWeiFilter);
			class onPage  {
				public void onClick(int arg0) {
					searchdata();

				}}
			final onPage pager=new onPage();
			bottomBeiJianDanWeiWindowLayout.findViewById(R.id.pageLeft).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					page--;
					if(page<0){
						page=0;
					}
					pager.onClick(0);
				}
			});
			bottomBeiJianDanWeiWindowLayout.findViewById(R.id.pageRight).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					page++;
					pager.onClick(0);
				}
			});
			//zl

			ViewGroup btnBottomBack = (ViewGroup) bottomBeiJianDanWeiWindowLayout
					.findViewById(R.id.btnBack);

			btnBottomBack.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					bottomWindow.dismiss();
				}
			});

			ViewGroup beiJianDanWeiBommtomSure = (ViewGroup) bottomBeiJianDanWeiWindowLayout
					.findViewById(R.id.btnSure);

			beiJianDanWeiBommtomSure.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {


					if(wvBeiJianDanWei.getTag()==null)return;
					int sel=java.lang.Integer.parseInt(String.valueOf(wvBeiJianDanWei.getTag()));
					TextInfo textInfo = (TextInfo) adpBeiJianDanWeiAdapter.getItem(sel);


					if (textInfo != null && textInfo.data != null) {

						dataBean.getData().setShouJianDanWei(
								(BeiJianDanWei) textInfo.data);

					} else {
						dataBean.getData().setShouJianDanWei(null);
					}

					// 如果存在受检单位
					if (dataBean.checkShouJianDanWei()) {


						int shouJianDanWeiCount =3;//
						// db.queryCountBeiJianDanWeiByName(dataBean.data
						//		.getShouJianDanWei().getName(),dataBean.data.getShouJianDanWei().getCode());

						// 如果受检单位不存在
						if (shouJianDanWeiCount >1) {

							// 用户从列表中选择,不创建,传true
							dataBean.data.getShouJianDanWei()
									.setCreateShouJianDanWei(false);

							//shuaixn
							sureExecuter.sureShouJianDanWei();
							//SerliazableUtil.saveTemp(dataBean.data, user.getUsername());
							bottomWindow.dismiss();

						}
					}




				}
			});
			// 检查单位根据名字进行过滤
			bottomBeiJianDanWeiWindowLayout.findViewById(R.id.btn_search).setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					page=0;
					pager.onClick(0);
				}});

			pager.onClick(0);
		}







	}

	/**
	 * 确认对象
	 *
	 * @author Mr
	 *
	 */
	private class SureExecuter {
		/**对象类型*/
		protected void sureDuxiangleixing(){

			onSureDuxiangleixing();

		}

		/**
		 * 确定检查行业
		 */
		private void sureJianChaHangYe() {

			TextView tvContentDescript = (TextView) rl_jiandujiancha_item_jianchahangye
					.findViewById(R.id.content_hangye);

			if (dataBean.checkJianChaHangYe()) {
				toggleIconChange(rl_jiandujiancha_item_jianchahangye, true);
				tvContentDescript.setText(dataBean.getJianChaHangYeString());

			} else {
				toggleIconChange(rl_jiandujiancha_item_jianchahangye, false);
				tvContentDescript.setText("");

			}

		}

		/**
		 * 确定区域,将区域信息书写到面板
		 */
		private void sureQuYu() {

			TextView tvContentDescript = (TextView) rl_chanpinjiance_item_suoshuquyu
					.findViewById(R.id.content_area);

			if (dataBean.checkQuYu()||plan!=null) {
				toggleIconChange(rl_chanpinjiance_item_suoshuquyu, true);

				if(plan!=null){
					tvContentDescript.setText(plan.optString("area_name"));
					Town[] tows=new Town[4];
					String tow=plan.optString("area");
					if(tow!=null){
						int tl=tow.length();
						if(tl>=4){
							tows[0]=new Town(tow.substring(0,4),"");
						}
						if(tl>=6){
							tows[1]=new Town(tow.substring(0,6),"");
						}
						if(tl>=8){
							tows[2]=new Town(tow.substring(0,8),"");
						}
						if(tl>=10){
							tows[3]=new Town(tow.substring(0,10),"");
						}
						if(tl>=12){
							tows[4]=new Town(tow.substring(0,12),"");
						}
					}
					dataBean.data.setTowns(tows);
				}else
					tvContentDescript.setText(dataBean.getQuYuString());

			} else {

				toggleIconChange(rl_chanpinjiance_item_suoshuquyu, false);

				tvContentDescript.setText("");

			}
		}

		private void sureShouJianDanWei() {

			TextView tvContentDescript = (TextView) rl_chanpinjiance_item_shoujiandanwei
					.findViewById(R.id.tvContentDescript);

			// 隐藏填写抽样单位的信息布局
			llCreateShouJianDanWeiLayout.setVisibility(View.GONE);

			if (dataBean.checkShouJianDanWei()) {

				toggleIconChange(rl_chanpinjiance_item_shoujiandanwei, true);

				tvContentDescript.setText(dataBean.data.getShouJianDanWei()
						.getName());
				int shouJianDanWeiCount =3;
				//				int shouJianDanWeiCount = db
				//						.queryCountBeiJianDanWeiByName(dataBean.data
				//								.getShouJianDanWei().getName(),dataBean.data
				//								.getShouJianDanWei().getCode());
				InitApplication.photolat=dataBean.data.getShouJianDanWei().getLatitude();
				InitApplication.photolng=dataBean.data.getShouJianDanWei().getLongitude();
				InitApplication.organization=dataBean.data.getShouJianDanWei().getName();
				if (shouJianDanWeiCount < 1) {
					// 隐藏填写抽样单位的信息布局
					llCreateShouJianDanWeiLayout.setVisibility(View.VISIBLE);
				} else {
					llCreateShouJianDanWeiLayout.setVisibility(View.GONE);
				}

			} else {
				toggleIconChange(rl_chanpinjiance_item_shoujiandanwei, false);
				tvContentDescript.setText("");
			}

		}


		/**
		 * 改变选择的图标
		 */
		public void toggleIconChange(ViewGroup viewGroup, boolean selected) {

			View ivChangeIcon = viewGroup.findViewWithTag("ivChangeIcon");

			Animation imageFadeIn = AnimationUtils.loadAnimation(ChanPinJianCeSelEntActivity.this,
					R.anim.icon_anim_move_x);

			if (ivChangeIcon != null && ivChangeIcon instanceof ImageView) {

				ImageView imageView = (ImageView) ivChangeIcon;

				/**
				 * 如果是更多则切换为
				 */
				if (selected) {
					imageView.setImageResource(R.drawable.item_select);
				} else {
					imageView
							.setImageResource(R.drawable.jiandujiancha_icon_more);
				}
				// 加载进入

				imageView.clearAnimation();

				imageView.setAnimation(imageFadeIn);

				imageFadeIn.start();
			}
		}

	}

	/**
	 * 数据对象
	 */
	private class DataBean {
		public String getDuixiangleixingStr() {

			StringBuffer jianChaNeiRongStr = new StringBuffer();

			for (int i = 0; i < data.getDuixiangleixing().size(); i++) {

				Duixiangleixing jianChaNeiRong = data.getDuixiangleixing()
						.get(i);

				jianChaNeiRongStr.append(jianChaNeiRong.getName());

				jianChaNeiRongStr.append(". ");



			}
			return jianChaNeiRongStr.toString();

		}
		//检查对象类型
		public boolean checkDuixiangleixing() {
			if(data==null||data.getDuixiangleixing()==null)return false;
			if (data.getDuixiangleixing().size() > 0) {
				return true;
			} else {
				return false;
			}
		}
		//检查对象类型
		public boolean checkPlan() {
			if(data==null||data.getPlan()==null)return false;
			return true;
		}
		//计划任务
		public Plan getPlan(){
			return data.getPlan();
		}
		/**
		 * 检查被检单位是否填写
		 *
		 * @return
		 */
		public boolean checkJianChaHangYe() {
			if (data.getJianChaHangYes().size() > 0) {
				return true;
			} else {
				return false;
			}
		}

		public String getJianChaHangYeString() {

			StringBuffer jianChaHangYeStr = new StringBuffer();

			for (int i = 0; i < data.getJianChaHangYes().size(); i++) {

				JianChaHangYe jianChaHangYe = data.getJianChaHangYes().get(i);

				jianChaHangYeStr.append(jianChaHangYe.getName());

				jianChaHangYeStr.append(". ");

				if (i < data.getJianChaHangYes().size() - 1) {
					jianChaHangYeStr.append("\n");
				}

			}

			return jianChaHangYeStr.toString();

		}
		private ChanPinJianCeRecord data = null;

		public ChanPinJianCeRecord getData() {
			return data;
		}
		public Town getTown() {

			for (Town town : data.getTowns()) {

				if (town != null&&town.getCode().length()>6) {
					return town;
				}

			}

			return new Town("3204","常州");

		}
		public boolean checkQuYu() {

			boolean result = false;

			for (Town town : data.getTowns()) {

				if (town != null&&town.getCode().length()>0) {
					result = true;

					break;
				}

			}

			return result;

		}

		public String getQuYuString() {

			StringBuffer result = new StringBuffer();

			for (int i = 0; i < data.getTowns().length; i++) {

				Town town = data.getTowns()[i];

				if (town != null) {

					if (!CharsetUtil.isNotNull(town.getName())) {

						town = db.queryTownById(town.getCode());
					}

					result.append(town.getName());
					result.append("--");
				}

			}

			if (result.length() > 0) {
				return result.substring(0, result.length() - 2);
			} else {

				return result.toString();
			}

		}

		public String getKnowCode() {
			if (data.getTowns()[3] != null) {

				return data.getTowns()[3].getCode();
			}else if (data.getTowns()[2] != null) {

				return data.getTowns()[2].getCode();
			} else if (data.getTowns()[1] != null) {

				return data.getTowns()[1].getCode();
			} else if (data.getTowns()[0] != null) {

				return data.getTowns()[0].getCode();
			} else {
				return null;
			}

		}

		public boolean checkChouYanJiGou() {
			if (data.getChouYanJiGou() != null) {
				return true;
			} else {
				return false;
			}

		}

		public String getChouYangJiGouString() {

			StringBuffer jianChaDanWeiTxt = new StringBuffer();

			ChouYanJiGou jianChaDanWei = data.getChouYanJiGou();

			jianChaDanWeiTxt.append(jianChaDanWei.getName());

			jianChaDanWeiTxt.append(". ");

			return jianChaDanWeiTxt.toString();

		}

		public boolean checkChouYangRenYuan() {
			if (data.getChouYangRenYuans().size() > 0) {
				return checkChouYangRenYuanCount();
			} else {
				return false;
			}

		}

		public boolean checkChouYangRenYuanOne() {
			if (data.getChouYangRenYuans().size() == 1) {
				return true;
			} else {
				return false;
			}

		}

		public boolean checkChouYangRenYuanCount() {
			if (data.getChouYangRenYuans().size() > 1) {
				return true;
			} else {
				return false;
			}

		}

		public String getChouYangRenYuanString() {

			StringBuffer chouYangRenYuanTxt = new StringBuffer();

			for (int i = 0; i < data.getChouYangRenYuans().size(); i++) {

				JianChaRenYuan chouYangRenYuans = data.getChouYangRenYuans()
						.get(i);

				chouYangRenYuanTxt.append(chouYangRenYuans.getName());

				chouYangRenYuanTxt.append(". ");

			}

			return chouYangRenYuanTxt.toString();

		}

		public boolean checkShouJianDanWei() {

			if (data.getShouJianDanWei() != null) {
				return true;
			} else {
				return false;
			}

		}

		public boolean checkTongXunDiZhi() {

			if (checkShouJianDanWei()
					&& CharsetUtil.isNotNull(data.getShouJianDanWei()
					.getAddress())) {
				return true;
			} else {
				return false;
			}

		}

		public boolean checkFuZeRen() {

			if (checkShouJianDanWei()
					&& CharsetUtil.isNotNull(data.getShouJianDanWei()
					.getCharge())) {
				return true;
			} else {
				return false;
			}

		}

		public boolean checkLianXiFangShi() {

			if (checkShouJianDanWei()
					&& CharsetUtil.isNotNull(data.getShouJianDanWei()
					.getMobile())) {
				return true;
			} else {
				return false;
			}

		}

		public boolean checkYangPinXinXi() {

			if (data.getYangPins().size() > 0) {

				return true;
			} else {

				return false;
			}

		}

		public String getYangPinXinXiString() {

			StringBuffer txtBuffer = new StringBuffer();

			for (int i = 0; i < data.getYangPins().size(); i++) {

				YangPin temp = data.getYangPins().get(i);

				txtBuffer.append(temp.getBianHaoString()
						+ "\u0020\u0020[\u0020" + temp.getName() + "\u0020]");

				if (i < data.getYangPins().size() - 1) {

					txtBuffer.append("\n");

				}

			}

			return txtBuffer.toString().trim();
		}

		public boolean checkRenWuLaiYuan() {

			if (data.getRenWuLaiYuan() != null) {

				return true;
			} else {

				return false;
			}

		}

		public String getRenWuLaiYuanStr() {

			StringBuffer txtBuffer = new StringBuffer();

			txtBuffer.append(data.getRenWuLaiYuan().getName());

			txtBuffer.append(". ");

			return txtBuffer.toString().trim();

		}

		public boolean checkJianCeLeiBie() {

			if (data.getJianCeLeiBie() != null) {

				return true;
			} else {

				return false;
			}

		}

		public String getJianCeLeiBieStr() {

			StringBuffer txtBuffer = new StringBuffer();

			JianCeLeiBie temp = data.getJianCeLeiBie();

			txtBuffer.append(temp.getName());

			txtBuffer.append(". ");

			return txtBuffer.toString().trim();

		}

		public boolean checkChanPinLeiBie() {

			if (data.getChanPinLeiBie() != null) {

				return true;
			} else {

				return false;
			}

		}

		public String getChanPinLeiBieStr() {

			StringBuffer txtBuffer = new StringBuffer();

			ChanPinLeiBie temp = data.getChanPinLeiBie();

			txtBuffer.append(temp.getName());

			txtBuffer.append(". ");

			return txtBuffer.toString().trim();

		}

		public boolean checkJianCeFangFa() {

			if (data.getJianCeFangFa() != null) {

				return true;
			} else {

				return false;
			}

		}

		public String getJianCeFangFaStr() {

			StringBuffer txtBuffer = new StringBuffer();

			JianCeFangFa temp = data.getJianCeFangFa();

			txtBuffer.append(temp.getName());

			txtBuffer.append(". ");

			return txtBuffer.toString().trim();

		}

		public boolean checkJianCeYiJu() {

			if (data.getJianCeYiJu() != null) {

				return true;
			} else {

				return false;
			}

		}

		public String getJianCeYiJuStr() {

			StringBuffer txtBuffer = new StringBuffer();

			JianCeYiJu temp = data.getJianCeYiJu();

			txtBuffer.append(temp.getName());

			txtBuffer.append(". ");

			return txtBuffer.toString().trim();

		}

		/**
		 * 检测拍照
		 */
		public boolean checkPaiZhao() {

			if (dataBean.data.getUploadPhoto().size() > 0) {
				return true;
			} else {
				return false;
			}

		}

		/**
		 * 检测签名
		 *
		 * @return
		 */
		public boolean checkQianMing() {

			if (CharsetUtil.isNotNull(data.getQianMingPath())) {
				return true;
			} else {
				return false;
			}

		}

	}

	/**
	 * 当数据返回的时候
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 提交数据
	 */
	public boolean submit() {

		if (!dataBean.checkQuYu()) {

			DialogUtil.showMessageDialog("请选择所属区域到区镇级..", null, this);

			return false;
		}
		if (!dataBean.checkShouJianDanWei()) {

			DialogUtil.showMessageDialog("请填写受检单位..", null, this);

			return false;
		}

		Toast.makeText(this, "asd", Toast.LENGTH_SHORT).show();

		return true;

	}



	@Override
	public void onDestroy() {

		if (db != null)
			db.close();
		super.onDestroy();
	}
	private int page=0;
	protected void onClickDuixiangleixing(final AlertDialog bottomWindow){

		bottomWindow.show();

		String[] kinds=null;
		if(dataBean.checkPlan())
			kinds=dataBean.getPlan().arrayEntKinds();

		/**
		 * 检查行业底部的布局
		 */
		ViewGroup bottomJianChaNeiRongWindowLayout = (ViewGroup) this
				.getLayoutInflater().inflate(
						R.layout.bottom_jianchaneirong, null);
		TextView bottomTitle=(TextView)bottomJianChaNeiRongWindowLayout.findViewById(R.id.bottomTitle);
		bottomTitle.setText("对象类型");
		// 让底部的宽度和屏幕的宽度一致
		bottomJianChaNeiRongWindowLayout.setMinimumWidth(ChanPinJianCeSelEntActivity.this
				.getWindowManager().getDefaultDisplay().getWidth());

		// 设置底部布局
		bottomWindow.setContentView(bottomJianChaNeiRongWindowLayout);

		// 弹出输入法的操作
		bottomWindow.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

		bottomWindow.getWindow().setGravity(Gravity.BOTTOM);

		/**
		 * 进行视图的动态生成
		 */
		ViewGroup jianChaNeiRongLayout = (ViewGroup) bottomJianChaNeiRongWindowLayout
				.findViewById(R.id.jianchaneironglayout);
		// 清理视图
		jianChaNeiRongLayout.removeAllViews();

		// 查询出所有检查行业
		List<Kv> jianChaNeiRongs = kinds==null?Dict.getKinds2():Dict.getKinds2(kinds);

		final List<CheckBox> cbsJianChaNeiRongs = new ArrayList<CheckBox>();

		// 遍历检查行业进行视图的生成
		for (final Kv temp1 : jianChaNeiRongs) {
			Duixiangleixing temp=new Duixiangleixing(temp1.getId(),temp1.getName());
			final RelativeLayout rootLayout = new RelativeLayout(
					this);

			MarginLayoutParams rootLayoutPrams = new MarginLayoutParams(
					MarginLayoutParams.MATCH_PARENT,
					MarginLayoutParams.WRAP_CONTENT);

			rootLayoutPrams.setMargins(0,
					Utils.dip2px(this, 3), 0,
					Utils.dip2px(this, 3));

			// 兼容低版本设置margin方法
			LayoutParams layoutPrarams = new LayoutParams(
					rootLayoutPrams);
			layoutPrarams.gravity = Gravity.TOP | Gravity.LEFT;

			rootLayout.setLayoutParams(layoutPrarams);
			rootLayout.setClickable(true);
			rootLayout.setGravity(Gravity.CENTER_VERTICAL);
			rootLayout.setPadding(Utils.dip2px(this, 8),
					Utils.dip2px(this, 5),
					Utils.dip2px(this, 8),
					Utils.dip2px(this, 5));
			rootLayout
					.setBackgroundResource(R.drawable.jiandujiancha_item_style);

			/*********************************************/
			TextView tvBeiJianChaHangYe = new TextView(this);

			RelativeLayout.LayoutParams tvBeiJianChaHangYeLayout = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			tvBeiJianChaHangYeLayout
					.addRule(RelativeLayout.CENTER_VERTICAL);
			tvBeiJianChaHangYe
					.setLayoutParams(tvBeiJianChaHangYeLayout);

			tvBeiJianChaHangYe.setPadding(
					Utils.dip2px(this, 8),
					Utils.dip2px(this, 8),
					Utils.dip2px(this, 8),
					Utils.dip2px(this, 8));
			tvBeiJianChaHangYe
					.setTextColor(Color.parseColor("#656769"));
			tvBeiJianChaHangYe.setTextSize(20);
			tvBeiJianChaHangYe.setText(temp.getName());

			rootLayout.addView(tvBeiJianChaHangYe);

			/*********************************************/

			CheckBox cbBeiJianChaNeiRong = new CheckBox(this);

			cbBeiJianChaNeiRong.setTag(temp);

			RelativeLayout.LayoutParams cbBeiJianChaHangYeLayout = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			cbBeiJianChaHangYeLayout
					.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			cbBeiJianChaHangYeLayout
					.addRule(RelativeLayout.CENTER_VERTICAL);
			cbBeiJianChaNeiRong
					.setLayoutParams(cbBeiJianChaHangYeLayout);




			if(dataBean==null||dataBean.getData()==null){
				return;
			}
			if (dataBean.getData().getDuixiangleixing().contains(temp)) {
				cbBeiJianChaNeiRong.setChecked(true);
			} else {

				cbBeiJianChaNeiRong.setChecked(false);
			}

			cbBeiJianChaNeiRong
					.setButtonDrawable(R.drawable.checkbox_select_style);

			rootLayout.addView(cbBeiJianChaNeiRong);

			rootLayout.setTag(cbBeiJianChaNeiRong);

			// 增加到缓存集合中
			cbsJianChaNeiRongs.add(cbBeiJianChaNeiRong);

			jianChaNeiRongLayout.addView(rootLayout);

			// 绑定事件,当布局被点击的时候选中checkbox
			rootLayout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					if (rootLayout.getTag() instanceof CheckBox) {

						CheckBox cbsJianChaNeiRong = (CheckBox) rootLayout
								.getTag();

						// 将指定项选中
						if (cbsJianChaNeiRong.isChecked()) {
							cbsJianChaNeiRong.setChecked(false);
						} else {
							cbsJianChaNeiRong.setChecked(true);
						}

					}

				}
			});

		}

		// 底部Window 的返回按钮
		ViewGroup btnBottomBack = (ViewGroup) bottomJianChaNeiRongWindowLayout
				.findViewById(R.id.btnBack);

		btnBottomBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				bottomWindow.dismiss();
			}
		});

		// 选择区域底部的确定按钮
		ViewGroup btnSureJianChaNeiRong = (ViewGroup) bottomJianChaNeiRongWindowLayout
				.findViewById(R.id.btnSureJianChaNeiRong);

		btnSureJianChaNeiRong.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dataBean.getData().getDuixiangleixing().clear();

				for (CheckBox checkBox : cbsJianChaNeiRongs) {

					if (checkBox.getTag() instanceof Duixiangleixing && checkBox.isChecked()) {

						Duixiangleixing kv = (Duixiangleixing) checkBox
								.getTag();
						dataBean.getData().getDuixiangleixing().add(kv);
					}

				}

				sureExecuter.sureDuxiangleixing();

				//	SerliazableUtil.saveTemp(dataBean.data, user.getUsername());
				// clearBeijingdanwei();
				bottomWindow.dismiss();

			}
		});

	}

	public void clearBeijingdanwei(){
		dataBean.data.setShouJianDanWei(null);
		sureExecuter.sureShouJianDanWei();
	}
	protected void onSureDuxiangleixing(){
		// 查询出监管内容的子项的进场或者退场，默认为进场动画


		TextView tvContentDescript = (TextView) rl_jiandujiancha_item_duixiangleixing
				.findViewById(R.id.content_duixiangleixing);


		if (dataBean.checkDuixiangleixing()) {
			toggleIconChange2(rl_jiandujiancha_item_duixiangleixing, true);
			tvContentDescript.setText(dataBean.getDuixiangleixingStr());


		}else{
			toggleIconChange2(rl_jiandujiancha_item_duixiangleixing, false);
			tvContentDescript.setText("");


		}
	}

	/**
	 * 改变选择的图标
	 */
	public void toggleIconChange2(ViewGroup viewGroup, boolean selected) {

		View ivChangeIcon = viewGroup.findViewWithTag("ivChangeIcon");

		Animation imageFadeIn = AnimationUtils.loadAnimation(this,
				R.anim.icon_anim_move_x);

		if (ivChangeIcon != null && ivChangeIcon instanceof ImageView) {

			ImageView imageView = (ImageView) ivChangeIcon;

			/**
			 * 如果是更多则切换为
			 */
			if (selected) {
				imageView.setImageResource(R.drawable.item_select);
			} else {
				imageView
						.setImageResource(R.drawable.jiandujiancha_icon_more);
			}
			// 加载进入

			imageView.clearAnimation();

			imageView.setAnimation(imageFadeIn);

			imageFadeIn.start();
		}
	}

	/**计划列表**/
	protected void onClickPlan(final AlertDialog bottomWindow){


	}
	public String[] bs=new String[]{ "32040201","32040401","32040501","32041107","32041201","32048101",
			"32048201","32040202","32040402","32040502","32041108","32041202","32048102",
			"32048202","32040203","32040403","32040503","32041109","32041203","32048104",
			"32048203","32040204","32040404","32041204","32048106",
			"32048204","32040205","32040405","32041205","32048110",
			"32048205","32040206","32040406","32041206",
			"32048206","32040207","32040407","32041207",
			"32048208","32040207","32040408","32041209","32041211","32041213","32041216"
	};
	private boolean allowCreate(String code){

		if(code.length()<8){return true;}
		code=code.substring(0, 8);
		for(int i=0;i<bs.length;i++){

			if(bs[i].equals(code))return false;
		}


		return true;


	}
	List<BeiJianDanWei> ents=new ArrayList<BeiJianDanWei>();
	EditText etBeiJianDanWeiFilter;
	ProgressDialog m_pDialog=null;
	protected void show(){
		m_pDialog = new ProgressDialog(this);
		m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

		m_pDialog.setTitle("提示");

		m_pDialog.setMessage("正在加载请稍后...");

		m_pDialog.setCancelable(true);

		m_pDialog.show();
	}
	protected void hide(){
		if(m_pDialog!=null){
			try{
				m_pDialog.hide();
			}catch(java.lang.Exception e){

			}finally{

				m_pDialog=null;
			}
		}
	}
	ListAdapter adpBeiJianDanWeiAdapter;
	private void searchdata(){

		String filter= String.valueOf(etBeiJianDanWeiFilter.getText());
		String code=user.getKnowRegion();
		try{
			code=dataBean.getData().getKnowTown().getCode();
		}catch(java.lang.Exception e){
			e.printStackTrace();
			code=user.getKnowRegion();
		}
		if(ChouyangrenwuActivity.getCheckPlan()!=null){
			code=ChouyangrenwuActivity.getCheckPlan().optString("area");
		}

		String url=Conf.v1609_root+ "/ent/keyword_general_search/region_code/"+code+"/limit/10/offset/"+(page*10)+"/username/"+user.getUsername()+"/from/1";
		if(filter!=null&&!filter.equals(""))
			url+="/keyword/"+filter;
		if(dataBean.checkDuixiangleixing()&&
				!dataBean.data.getDuixiangleixingStr().equals("")){
			url+="/kind/"+dataBean.data.getDuixiangleixingStr();

		}
		if(dataBean.checkJianChaHangYe()&&
				!dataBean.data.getJianChaHangYesStr().equals("")){
			url+="/category/"+dataBean.data.getJianChaHangYesStr();

		}

		Log.d("debug",String.valueOf(url));
		AsyncClienter.get(url ,new JsonHttpResponseHandler () {
			@Override
			public void onSuccess(int statusCode,Header[] headers,JSONObject d)
			{
				super.onSuccess(statusCode, headers, d);
				Log.d("debug",String.valueOf(d));
				JSONArray objents=d.optJSONArray("result");


				if(objents==null)return;

				ArrayList<TextInfo> wvBeiJianDanWeiDatas = new ArrayList<TextInfo>();


				for(int i=0;i<objents.length();i++){
					JSONObject c2=objents.optJSONObject(i);
					BeiJianDanWei b=new BeiJianDanWei();
					b.setId(c2.optString("id"));
					b.setName(c2.optString("name"));
					b.setAddress(c2.optString("address"));
					b.setCharge(c2.optString("charge"));
					b.setTel(c2.optString("contact"));
					b.setD(c2.optString("d"));
					b.setKind(c2.optInt("kind"));
					b.setLatitude(c2.optDouble("latitude"));
					b.setLongitude(c2.optDouble("longitude"));
					b.setCode(c2.optString("region_code"));
					TextInfo textInfo = new TextInfo(
							b.getId(),
							b.getName(), b);

					wvBeiJianDanWeiDatas.add(textInfo);
				}
				adpBeiJianDanWeiAdapter.setData(wvBeiJianDanWeiDatas);


			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
								  Throwable error,JSONObject data) {
				super.onFailure(statusCode, headers,   error,data);
				Toast.makeText(ChanPinJianCeSelEntActivity.this, "服务器繁忙,请稍后重试", Toast.LENGTH_SHORT).show();

			}
			@Override
			public void onStart()
			{
				super.onStart();
				show();
			}
			@Override
			public void onFinish()
			{  super.onFinish();
				hide();
			}

		});


	}
}
